package com.qulai.amazon_us.service.basic.impl;

import java.util.List;

import com.qulai.amazon_us.domain.basic.Image;
import com.qulai.amazon_us.domain.basic.dto.ProductDto;
import com.qulai.amazon_us.mapper.basic.ImageMapper;
import com.qulai.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qulai.amazon_us.mapper.basic.ProductMapper;
import com.qulai.amazon_us.domain.basic.Product;
import com.qulai.amazon_us.service.basic.IProductService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 商品库存Service业务层处理
 *
 * @author hunan
 * @date 2025-01-03
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ImageMapper imageMapper;

    /**
     * 查询商品库存
     *
     * @param id 商品库存主键
     * @return 商品库存
     */
    @Override
    public Product selectProductById(String id) {
        return productMapper.selectProductById(id);
    }

    @Override
    public Product selectProductBySku(String sku) {
        return productMapper.selectProductBySku(sku);
    }

    /**
     * 查询商品库存列表
     *
     * @param product 商品库存
     * @return 商品库存
     */
    @Override
    public List<Product> selectProductList(ProductDto product) {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增商品库存
     *
     * @param product 商品库存
     * @return 结果
     */
    @Override
    public int insertProduct(Product product) {
        product.setCreateTime(DateUtils.getNowDate());
        return productMapper.insertProduct(product);
    }

    /**
     * 修改商品库存
     *
     * @param product 商品库存
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateProduct(Product product) {

        try {
            product.setUpdateTime(DateUtils.getNowDate());
            Image i =  imageMapper.selectImageByBasicId(Long.parseLong(product.getId()));
            if (i != null) {
                i.setStatus(product.getStatus());
                i.setPublished(product.getPublished());
                i.setUpdateTime(DateUtils.getNowDate());
                imageMapper.updateImage(i);
            }
            product.setUpdateTime(DateUtils.getNowDate());
            return productMapper.updateProduct(product);

        } catch (Exception e) {
            // 手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }


    }

    /**
     * 批量删除商品库存
     *
     * @param ids 需要删除的商品库存主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(String[] ids) {
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 删除商品库存信息
     *
     * @param id 商品库存主键
     * @return 结果
     */
    @Override
    public int deleteProductById(String id) {
        return productMapper.deleteProductById(id);
    }



    @Override
    public int selectProductRestNum(String brand, List<String> notClassNameList, int times, String tables,List<String> basicPlatformList,String basicProductIndex){
        return  productMapper.selectProductRestNum(brand, notClassNameList,times,tables,basicPlatformList,basicProductIndex);
    }
}
